package org.charless.skeleton.rpc;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.charless.skeleton.UserManager;
import org.charless.spring.jsonrpc.JsonRpcController;

import com.metaparadigm.jsonrpc.JSONRPCBridge;


public class JsonRpcControllerImpl extends JsonRpcController implements SessionManagement
{
	private Log logger = LogFactory.getLog(JsonRpcControllerImpl.class);
	
	@Resource(name="UserManager")
	private UserManager UserManager;
	
	public JsonRpcControllerImpl()
	{
		super();
	}
	
	protected  void registerSessionAjaxServicesImpl(JSONRPCBridge bridge) throws Exception{
		bridge.enableReferences();
		//bridge.registerCallableReference(.class);

		// CLIENT MANAGER
		logger.info("Registering UserManager '"+this.UserManager.toString()+"' to JSON Session bridge '"+bridge.toString()+"'");
		bridge.registerObject("UserManager", this.UserManager,org.charless.skeleton.UserManager.class);
		
	}
	
	/****************************************************************************
	/* USER MANAGER                                                   			*
	/****************************************************************************/
	private String getCurrentUser() {
		return getHttpServletRequest().getRemoteUser();
	}
	
	/****************************************************************************
	/* SESSION                                                       			*
	/****************************************************************************/
	/**
     * Useless function called to restart the server session timeout
     */
    public void refreshSession()
    {
    	
    }
    
    /**
     * Invalidate the session
     */
    public void invalidateSession() {
    	HttpSession session = this.getHttpServletRequest().getSession();
    	logger.info("Closing session: "+session.getId());
    	session.invalidate();
    }

	public void setUserManager(UserManager UserManager) {
		this.UserManager = UserManager;
	}
}
